package org.techmore.dataaccessobject;

import org.techmore.bussiness.QuestionsBeanDataHandler;
import org.techmore.common.utils.database.DatabaseHelper;
import org.techmore.common.utils.database.SqlitePropertiesUtil;
import org.techmore.common.utils.properties.PropertiesUtil;
import org.techmore.valueobject.QuestionVO;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ChallengeMainActivityDAO {
	
	private SQLiteDatabase sqLiteDatabase = null;
	
	public boolean setQuestionListContent(Context context){
		boolean success = false;
		QuestionsBeanDataHandler questionsBeanDataHandler = QuestionsBeanDataHandler.getInstance();
		String sql = SqlitePropertiesUtil.getInstance().getValue("questions_select");
		
		Cursor cursor = null;
		QuestionVO questionVO = null;
		
		try {
			sqLiteDatabase = new DatabaseHelper(context, PropertiesUtil.getInstance().getValue("databaseName")).getReadableDatabase();
			cursor = sqLiteDatabase.rawQuery(sql, new String[]{});
			while(cursor.moveToNext()){
				questionVO = new QuestionVO();
				////id,questionContent,answerA,answerB,answerC,trueAnswer,answerDescription,imageName
				questionVO.setId(cursor.getString(cursor.getColumnIndex("ID")));
				questionVO.setQuestionContent(cursor.getString(cursor.getColumnIndex("questionContent")));
				questionVO.setAnswerA(cursor.getString(cursor.getColumnIndex("answerA")));
				questionVO.setAnswerB(cursor.getString(cursor.getColumnIndex("answerB")));
				questionVO.setAnswerC(cursor.getString(cursor.getColumnIndex("answerC")));
				questionVO.setTrueAnswer(cursor.getString(cursor.getColumnIndex("trueAnswer")));
				questionVO.setAnswerDescription(cursor.getString(cursor.getColumnIndex("answerDescription")));
				questionVO.setImageName(cursor.getString(cursor.getColumnIndex("imageName")));
				
				questionsBeanDataHandler.addQuestion(questionVO);
			}
			success = true;
		} catch (Exception e) {
			Log.e("sqlite:", e.getMessage());
		}finally{
			cursor.close();
			sqLiteDatabase.close();
		}
		return success;
	}

}
